#include "CwRect.h"

/* prototype declarations */
PtWidgetClass_t *CreateCwRectClass (void);

/* widget class pointer - class structure, create function */
PtWidgetClassRef_t WCwRect = { NULL, CreateCwRectClass };
PtWidgetClassRef_t *CwRect = &WCwRect; 


//-----------------------------------------------------------------------------
// загрузка значений по умолчанию
//-----------------------------------------------------------------------------
static void cwrect_dflts (PtWidget_t *widget)
{
	CwRectWidget_t		*rect 	= ( CwRectWidget_t * ) widget;
//	PtBasicWidget_t		*basic 	= ( PtBasicWidget_t * ) widget;

	rect->rect_color = 0xff0000;
	rect->draw_rect = 0;
	rect->type = thiristor;
}

//-----------------------------------------------------------------------------
// функция прорисовки всего widget'a
//-----------------------------------------------------------------------------
static void cwrect_draw (PtWidget_t *widget, PhTile_t *damage)
{
	CwRectWidget_t		*rect 	= ( CwRectWidget_t * ) widget;
	PtBasicWidget_t		*basic 	= ( PtBasicWidget_t * ) widget;
	PhRect_t			canvas, r;
	PhPoint_t			pt1, pt2;

	PgSetDrawMode (Pg_DRAWMODE_OPAQUE);
	PtSuperClassDraw (PtBasic, widget, damage );
	PtCalcCanvas (widget, &canvas);

	PtClipAdd (widget, &canvas);

	
	if (rect->draw_rect) {
		PgSetStrokeWidth (2);			
		PgSetStrokeColor (rect->rect_color);
		r = canvas;	
		r.ul.x += 1;
		r.ul.y += 2;
		r.lr.x -= 0;
		r.lr.y -= 0;
		PgDrawRect (&r, Pg_DRAW_STROKE);
	}

	PgSetStrokeWidth (2);	
	PgSetStrokeColor (basic->color);
	switch (rect->type) {	
		//--------------------------------------------------------------------	
		case thiristor:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/5;
			PgDrawLine (&pt1, &pt2);		

			pt1.x = canvas.lr.x - (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/5;
			PgDrawLine (&pt1, &pt2);		

			pt1.x = canvas.lr.x - (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/5;
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);		

			pt1.x = canvas.lr.x - (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/5;
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);		

			pt1.x = canvas.lr.x - (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = pt1.x + (canvas.lr.x - canvas.ul.x)/10;
			pt2.y = pt1.y + (canvas.lr.y - canvas.ul.y)/10;
			PgDrawLine (&pt1, &pt2);		
			break;
		//--------------------------------------------------------------------				
		case open_contact:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y;
			pt2.x = pt1.x;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)*17/60; // 1/3 + 5%
			PgDrawLine (&pt1, &pt2);
			break;
		//--------------------------------------------------------------------	
		case closed_contact:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y;
			pt2.x = pt1.x;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)*17/60; // 1/3 - 5%
			PgDrawLine (&pt1, &pt2);			

			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)*24/80; // 1/4 + 5%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);	
			break;
		//--------------------------------------------------------------------	
		case dc_motor:
			PgSetStrokeWidth (1);	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/2;
			pt2.x = (canvas.lr.x - canvas.ul.x)/3;
			pt2.y = (canvas.lr.y - canvas.ul.y)/3;
			PgDrawEllipse (&pt1, &pt2, Pg_DRAW_STROKE);
			
			r.ul.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2 - 5;
			r.ul.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/6 + 2;
			r.lr.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2 + 5;
			r.lr.y = r.ul.y - 5;
			PgDrawRect (&r, Pg_DRAW_STROKE);
			
			r.ul.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2 - 5;
			r.ul.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/6 - 2;
			r.lr.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2 + 5;
			r.lr.y = r.ul.y + 5;
			PgDrawRect (&r, Pg_DRAW_STROKE);
			break;
		//--------------------------------------------------------------------	
		case switch_left:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)*17/60; // 1/3 - 5%
			PgDrawLine (&pt1, &pt2);			

			pt1.x = canvas.ul.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x + (canvas.lr.x - canvas.ul.x)*28/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);	
			
			pt1.x = canvas.lr.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)*30/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);				
			break;			
		//--------------------------------------------------------------------	
		case switch_right:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x + (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)*17/60; // 1/3 - 5%
			PgDrawLine (&pt1, &pt2);			

			pt1.x = canvas.ul.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x + (canvas.lr.x - canvas.ul.x)*28/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);	
			
			pt1.x = canvas.lr.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)*30/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);				
			break;		
		//--------------------------------------------------------------------	
		case switch_mid:
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.lr.y;
			pt2.x = pt1.x;
			pt2.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			PgDrawLine (&pt1, &pt2);
	
			pt1.x = canvas.ul.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x + (canvas.lr.x - canvas.ul.x)*28/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);	
			
			pt1.x = canvas.lr.x;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = pt1.x - (canvas.lr.x - canvas.ul.x)*30/80; // 1/4 + 10%
			pt2.y = pt1.y;
			PgDrawLine (&pt1, &pt2);				
			break;								
		//--------------------------------------------------------------------	
		case transformator:
			PgSetStrokeWidth (1);	
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/2;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = (canvas.lr.y - canvas.ul.y)/4;
			PgDrawEllipse (&pt1, &pt2, Pg_DRAW_STROKE);
			
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/3;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = (canvas.lr.y - canvas.ul.y)/4;
			PgDrawEllipse (&pt1, &pt2, Pg_DRAW_STROKE);

			pt1.x = canvas.lr.x - (canvas.lr.x - canvas.ul.x)/3;
			pt1.y = canvas.lr.y - (canvas.lr.y - canvas.ul.y)/3;
			pt2.x = (canvas.lr.x - canvas.ul.x)/4;
			pt2.y = (canvas.lr.y - canvas.ul.y)/4;
			PgDrawEllipse (&pt1, &pt2, Pg_DRAW_STROKE);	
			break;
		//--------------------------------------------------------------------	
		case obm_vozb:
			PgSetStrokeWidth (1);
			pt1.x = canvas.ul.x + (canvas.lr.x - canvas.ul.x)/5;
			pt1.y = canvas.ul.y + (canvas.lr.y - canvas.ul.y)/5;
			pt2.x = (canvas.lr.x - canvas.ul.x)*3/5;
			pt2.y = (canvas.lr.y - canvas.ul.y)/10;
			
			PgDrawArc (&pt1, &pt2, 0xC000, 0x4000, Pg_ARC | Pg_DRAW_STROKE);
			pt1.y += (canvas.lr.y - canvas.ul.y)/5;
			PgDrawArc (&pt1, &pt2, 0xC000, 0x4000, Pg_ARC | Pg_DRAW_STROKE);
			pt1.y += (canvas.lr.y - canvas.ul.y)/5;
			PgDrawArc (&pt1, &pt2, 0xC000, 0x4000, Pg_ARC | Pg_DRAW_STROKE);
			pt1.y += (canvas.lr.y - canvas.ul.y)/5;
			PgDrawArc (&pt1, &pt2, 0xC000, 0x4000, Pg_ARC | Pg_DRAW_STROKE);
			
			break;			
	}


	PtClipRemove ();
}

//-----------------------------------------------------------------------------
// создание класса
//-----------------------------------------------------------------------------
PtWidgetClass_t *CreateCwRectClass( void )
{
	// define our resources
	static PtResourceRec_t resources[] = {
		{Cw_RECT_ARG_TYPE, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_NUMBER (CwRectWidget_t, type), 0},
		{Cw_RECT_ARG_DRAW_RECT, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_NUMBER(CwRectWidget_t, draw_rect), 0},
		{Cw_RECT_ARG_COLOR, Pt_CHANGE_REDRAW, 0, 
			Pt_ARG_IS_NUMBER (CwRectWidget_t, rect_color), 0},			
	};

	// set up our class member values
	static PtArg_t args[] = {
		{ Pt_SET_VERSION, 110},
		{ Pt_SET_STATE_LEN, sizeof (CwRectWidget_t) },
		{ Pt_SET_DFLTS_F, (long)cwrect_dflts },
		{ Pt_SET_DRAW_F, (long)cwrect_draw },
		{ Pt_SET_FLAGS, 0, Pt_RECTANGULAR },
		{ Pt_SET_NUM_RESOURCES, sizeof (resources)/sizeof (resources [0])},
		{ Pt_SET_RESOURCES, (long)resources, sizeof (resources)/sizeof (resources [0])},
	};

	// create the widget class
	return (CwRect->wclass = PtCreateWidgetClass (
		PtBasic, 0, sizeof (args)/sizeof (args[0]), args));
}